package com.adcc.swingui.conf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.adcc.swingui.bean.ConfigLayersBean;
import com.adcc.swingui.bean.ConfigLayersNode;

public class SystemSetConf {
	private static Logger logger = Logger.getLogger(PanelConf.class);
	private static List<ConfigLayersNode> nodes;
	private static Map<String, ConfigLayersBean> systemSetConf = new HashMap<String, ConfigLayersBean>();
	private static List<String> authorityList = null;

	public static void initConf(String path) {
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(path);
			Element root = (Element) doc.getRootElement();
			if (!root.getName().equals("root")) {
				logger.error("读取root根节点异常");
			}
			if (root != null) {
				ConfigLayersBean bean = new ConfigLayersBean();
				for (Iterator i = root.elementIterator(); i.hasNext();) {
					Element systemSet = (Element) i.next();
					List<ConfigLayersNode> componentList = new ArrayList<ConfigLayersNode>();
					if (systemSet.getName().equals("systemSet")) {
						bean.setName(systemSet.attributeValue("name"));
						bean.setI18nName(systemSet.attributeValue("i18nName"));
						bean.setDefaultLayerName(systemSet
								.attributeValue("defaultLayerName"));
						ConfigLayersNode configLayersNode = new ConfigLayersNode(
								null, null);
						for (Iterator j = systemSet.elementIterator(); j
								.hasNext();) {
							Element component = (Element) j.next();
							readNode(component, configLayersNode);
						}
						nodes = configLayersNode.getSubNodes();
						bean.setNodes(nodes);
					}
					systemSetConf.put(bean.getName(), bean);
				}

			}

		} catch (DocumentException e) {
			logger.error(path + "解析失败严重错误" + path);
		}
	}

	public static void initConf(String path, List<String> privilege) {
		try {
			authorityList = privilege;
			SAXReader reader = new SAXReader();
			Document doc = reader.read(path);
			Element root = (Element) doc.getRootElement();
			if (!root.getName().equals("root")) {
				logger.error("读取root根节点异常");
			}
			if (root != null) {
				ConfigLayersBean bean = new ConfigLayersBean();
				for (Iterator i = root.elementIterator(); i.hasNext();) {
					Element systemSet = (Element) i.next();
					List<ConfigLayersNode> componentList = new ArrayList<ConfigLayersNode>();
					if (systemSet.getName().equals("systemSet")) {
						bean.setName(systemSet.attributeValue("name"));
						bean.setI18nName(systemSet.attributeValue("i18nName"));
						bean.setDefaultLayerName(systemSet
								.attributeValue("defaultLayerName"));
						ConfigLayersNode configLayersNode = new ConfigLayersNode(
								null, null);
						for (Iterator j = systemSet.elementIterator(); j
								.hasNext();) {
							Element component = (Element) j.next();
							readNode(component, configLayersNode);
						}
						nodes = configLayersNode.getSubNodes();
						bean.setNodes(nodes);
					}
					systemSetConf.put(bean.getName(), bean);
				}

			}

		} catch (DocumentException e) {
			logger.error(path + "解析失败严重错误" + path);
		}
	}

	/**
	 * 解析设置配置文件
	 * 
	 * @param ele
	 * @param node
	 */
	public static void readNode(Element ele, ConfigLayersNode node) {
		ConfigLayersNode childNode = null;
		boolean visible = true;
		String privilegestr = "";

		if ("ConfigLayersNode".equals(ele.getName())) {
			String name = ele.attributeValue("name");
			String panel = ele.attributeValue("panel");
			String i18nName = ele.attributeValue("i18nName");
			privilegestr = ele.attributeValue("privilegestr");
			visible = Boolean.parseBoolean(ele.attributeValue("visible"))
					&& haveAuthority(privilegestr);
			childNode = new ConfigLayersNode(name, panel);
			childNode.setI18nName(i18nName);
			if (visible) {
				node.addSubNode(childNode);
			}
		}
		Element element = null;
		if (visible) {
			for (Iterator i = ele.elementIterator("ConfigLayersNode"); i
					.hasNext();) {
				element = (Element) i.next();
				if (childNode != null) {
					readNode(element, childNode);
				} else {
					readNode(element, node);
				}
			}
		}

	}

	private static boolean haveAuthority(String pri) {
		return pri == null || pri.trim().equals("") || authorityList == null
				|| authorityList.contains(pri.trim().equals(""));
	}

	public static void main(String[] args) {
		initConf("conf/default/systemset.xml");
		Set<String> keySet = systemSetConf.keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			String key = it.next();
			ConfigLayersBean value = systemSetConf.get(key);
			List<ConfigLayersNode> a = new ArrayList<ConfigLayersNode>();
			a = value.getNodes();
			if (a != null && a.size() != 0) {

				for (int i = 0; i < a.size(); i++) {
					ConfigLayersNode comp = new ConfigLayersNode(null, null);
					comp = a.get(i);
				}
			}
		}
	}

	public static Map<String, ConfigLayersBean> getSystemsetConf() {
		return systemSetConf;
	}

}
